define(['dojo/_base/declare'], function(declare) {
  'use strict';
  return declare(null, {
    startColor: null,
    endColor: null,
    step: null,
    colorArr: [],
    /**
     * 参数：
     * startColor：开始颜色hex
     * endColor：结束颜色hex
     * step:几个阶级（几步）
     */
    constructor(startColor, endColor, step) {
      this.startColor = startColor;
      this.endColor = endColor;
      this.step = step;
    },
    getGradientColorArr: function(startColor, endColor, step) {
      this.colorArr = [];
      this.startColor = startColor;
      this.endColor = endColor;
      this.step = step;

      var startRGB = this.colorRgb(this.startColor); //转换为rgb数组模式
      var startR = startRGB[0];
      var startG = startRGB[1];
      var startB = startRGB[2];

      var endRGB = this.colorRgb(this.endColor);
      var endR = endRGB[0];
      var endG = endRGB[1];
      var endB = endRGB[2];

      var sR = (endR - startR) / this.step; //总差值
      var sG = (endG - startG) / this.step;
      var sB = (endB - startB) / this.step;

      var rgbArr = [];
      for (var i = 0; i < step; i++) {
        // //计算每一步的hex值
        // var hex = this.colorHex(
        //   'rgb(' +
        //     parseInt(sR * i + startR) +
        //     ',' +
        //     parseInt(sG * i + startG) +
        //     ',' +
        //     parseInt(sB * i + startB) +
        //     ')'
        // );
        // this.colorArr.push(hex);
        rgbArr = [];
        rgbArr = [parseInt(sR * i + startR), parseInt(sG * i + startG), parseInt(sB * i + startB)];
        this.colorArr.push(rgbArr);
      }
      return this.colorArr;
    },
    // 将hex表示方式转换为rgb表示方式(这里返回rgb数组模式)
    colorRgb: function(sColor) {
      var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
      var sColor = sColor.toLowerCase();
      if (sColor && reg.test(sColor)) {
        if (sColor.length === 4) {
          var sColorNew = '#';
          for (var i = 1; i < 4; i += 1) {
            sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1));
          }
          sColor = sColorNew;
        }
        //处理六位的颜色值
        var sColorChange = [];
        for (var i = 1; i < 7; i += 2) {
          sColorChange.push(parseInt('0x' + sColor.slice(i, i + 2)));
        }
        return sColorChange;
      } else {
        return sColor;
      }
    },
    // 将rgb表示方式转换为hex表示方式
    colorHex: function(rgb) {
      var _this = rgb;
      var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
      if (/^(rgb|RGB)/.test(_this)) {
        var aColor = _this.replace(/(?:(|)|rgb|RGB)*/g, '').split(',');
        var strHex = '#';
        for (var i = 0; i < aColor.length; i++) {
          var hex = Number(aColor[i]).toString(16);
          hex = hex < 10 ? 0 + '' + hex : hex; // 保证每个rgb的值为2位
          if (hex === '0') {
            hex += hex;
          }
          strHex += hex;
        }
        if (strHex.length !== 7) {
          strHex = _this;
        }
        return strHex;
      } else if (reg.test(_this)) {
        var aNum = _this.replace(/#/, '').split('');
        if (aNum.length === 6) {
          return _this;
        } else if (aNum.length === 3) {
          var numHex = '#';
          for (var i = 0; i < aNum.length; i += 1) {
            numHex += aNum[i] + aNum[i];
          }
          return numHex;
        }
      } else {
        return _this;
      }
    }
  });
});
